package com.seventeen.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.seventeen.pojo.Player;
import com.seventeen.utils.JDBCUtils;

public class UserDao {
	private static Connection conn = JDBCUtils.getConnection();
	private static PreparedStatement pstmt = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;

	public int register(Player user) {

		String sql = "insert into user(name,username,password) values(?,?,?)";
		int result = 0;
		try {
			pstmt = conn.prepareStatement(sql);

			pstmt.setString(1, user.getName());
			pstmt.setString(2, user.getUsername());
			pstmt.setString(3, user.getPassword());

			result = pstmt.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;

	}

	public boolean login(Player user) {
		String sql = "select username,password from user where username=? and password=?";
		boolean wasNull = true;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getUsername());
			pstmt.setString(2, user.getPassword());
			rs = pstmt.executeQuery();
			wasNull = rs.wasNull(); // false
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wasNull;
	}
	
	public static void close(){
		JDBCUtils.close(rs, stmt, pstmt, conn);
	}

}
